package com.xinghai.system.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinghai.common.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import com.xinghai.system.mapper.XhVehicleOrderHisMapper;
import com.xinghai.system.domain.XhVehicleOrderHis;
import com.xinghai.system.service.IXhVehicleOrderHisService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 用车订单分配记录Service业务层处理
 * 
 * @author xinghai
 * @date 2024-07-23
 */
@Service
public class XhVehicleOrderHisServiceImpl extends ServiceImpl<XhVehicleOrderHisMapper, XhVehicleOrderHis> implements IXhVehicleOrderHisService
{
    @Autowired
    private XhVehicleOrderHisMapper xhVehicleOrderHisMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询用车订单分配记录
     * 
     * @param id 用车订单分配记录主键
     * @return 用车订单分配记录
     */
    @Override
    public XhVehicleOrderHis getInfo(Long id)
    {
        return xhVehicleOrderHisMapper.getInfo(id);
    }

    /**
     * 查询用车订单分配记录列表
     * 
     * @param xhVehicleOrderHis 用车订单分配记录
     * @return 用车订单分配记录
     */
    @Override
    public List<XhVehicleOrderHis> list(XhVehicleOrderHis xhVehicleOrderHis)
    {
            xhVehicleOrderHis.setDelFlag("0");
        return xhVehicleOrderHisMapper.list(xhVehicleOrderHis);
    }

    /**
     * 新增用车订单分配记录
     * 
     * @param xhVehicleOrderHis 用车订单分配记录
     * @return 结果
     */
    @Override
    public int insert(XhVehicleOrderHis xhVehicleOrderHis)
    {
        xhVehicleOrderHis.setCreateTime(DateUtils.getNowDate());
        return xhVehicleOrderHisMapper.insert(xhVehicleOrderHis);
    }
    /**
     * 批量新增用车订单分配记录
     *
     * @param xhVehicleOrderHisList 用车订单分配记录
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhVehicleOrderHis> xhVehicleOrderHisList)
    {
        return xhVehicleOrderHisMapper.insertBatch(xhVehicleOrderHisList);
    }
    /**
     * 修改用车订单分配记录
     * 
     * @param xhVehicleOrderHis 用车订单分配记录
     * @return 结果
     */
    @Override
    public int update(XhVehicleOrderHis xhVehicleOrderHis)
    {
        xhVehicleOrderHis.setUpdateTime(DateUtils.getNowDate());
        return xhVehicleOrderHisMapper.update(xhVehicleOrderHis);
    }

    /**
     * 批量删除用车订单分配记录
     * 
     * @param ids 需要删除的用车订单分配记录主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return xhVehicleOrderHisMapper.deleteBatch(ids);
    }

    /**
     * 删除用车订单分配记录信息
     * 
     * @param id 用车订单分配记录主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return xhVehicleOrderHisMapper.delete(id);
    }

    /**
     * 导入用车订单分配记录信息
     *
     * @param list 用车订单分配记录信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhVehicleOrderHis> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhVehicleOrderHisMapper xhVehicleOrderHisMapper = sqlSession.getMapper(XhVehicleOrderHisMapper.class);
            list.stream().forEach(item -> xhVehicleOrderHisMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public int selectVehicleOrderHis(Long vehicleId) {
        QueryWrapper<XhVehicleOrderHis> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("vehicle_id", vehicleId);
        XhVehicleOrderHis xhVehicleOrderHis = xhVehicleOrderHisMapper.selectOne(queryWrapper);
        return xhVehicleOrderHisMapper.insert(xhVehicleOrderHis);
    }
}
